package com.it.aaaawebmaigc;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.microsoft.schemas.vml.STExt;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
import java.net.URL;
import java.util.*;


@SpringBootApplication
public class AaaaWebmaigcApplication {

    public static void main(String[] args) throws IOException
    {
        SpringApplication.run(AaaaWebmaigcApplication.class, args);

        Map<Integer, String> map =new LinkedHashMap<>();
        List<Map<Integer, String>> dataPojo = new ArrayList<>();

        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("D:\\data.xls");
        //跳过当前行，既第一行，非必须，在此演示用
        writer.passCurrentRow();
        // 设置宽度
        for (int i = 0; i <10 ; i++) {
            writer.setColumnWidth(i,40);
        }
        writer.setDefaultRowHeight(17);


        //爬取的地址
        String url ="https://www.aquanliang.com/blog";
        Document document= Jsoup.parse(new URL(url),30000);
        //拿到了整个目标网页的目标内容
        /*         进行分析 第一页如图所示。
          img
         *  图片    2ahG-zumH_-g0nsl6xhsF0s
          class
         *  标题    _3_JaaUmGUCjKZIdiLhqtfr
         *  时间     _3TzAhzBA-XQQruZs-bwWjE
         *  阅读数    _2gvAnxa4Xc7IT14d5w8MI1
         * */

        //标题
        Elements title = document.getElementsByClass("_3_JaaUmGUCjKZIdiLhqtfr");
        String[] titlearr = title.text().split(" ");
        //消除简短的句子。例如： |  客户。。
    int num =0;
        for (int i = 0; i < titlearr.length ; i++) {
            if (titlearr[i].length()<=8||titlearr[i].contains("|")){
            }else{
                System.out.println(titlearr[i]);
                 map.put(num,titlearr[i]);
                 num++;
            }
        }
        dataPojo.add(map);
        writer.write(dataPojo, true);
        dataPojo.clear();
      //  System.out.println(dataPojo.size());
       //时间
        Elements alltime =document.getElementsByClass("_3TzAhzBA-XQQruZs-bwWjE");
        String[] timearr = alltime.text().split(" ");
        System.out.println(timearr[0]);
        for (int i = 0; i <timearr.length; i++) {
            map.put(i,timearr[i]);
        }
        dataPojo.add(map);
        writer.write(dataPojo, false);
        dataPojo.clear();
   //     System.out.println(dataPojo.size());
        //阅读数
        Elements read =document.getElementsByClass("_2gvAnxa4Xc7IT14d5w8MI1");
        String[] readarr = read.text().split(" ");
        System.out.println(readarr[0]);

        for (int i = 0; i <readarr.length ; i++) {
            map.put(i,readarr[i]);
        }
        dataPojo.add(map);
        writer.write(dataPojo, false);
        dataPojo.clear();

        //图片地址
        Elements imgs = document.getElementsByTag("img");
        int num1=0;
        for(Element element : imgs) {
            //获取src属性的值
            String imgSrc=element.attr("src");
            //以http开头
            if(imgSrc.startsWith("h")){
                String[] imgarr = imgSrc.split(" ");
                System.out.println(imgarr[0]);

                map.put(num1,imgarr[0]);
                 num1++;
            }
        }
        dataPojo.add(map);
        writer.write(dataPojo, false);

        //关闭writer，释放内存
        writer.close();
        System.out.println("成功");
    }



}



